// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CoffeeScript.cs" company="Jorje Nava">
//   Copyright (c) 2011, Jorje Nava
//   All rights reserved.
//   Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
//
//   * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
//   * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
//   * Neither the name of Jorje Nava nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
//
//   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
//   THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
//   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
//   BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
//   EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
// </copyright>
// <summary>
//   Defines the PluginBase type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace NppPluginNET
{
    using System;
    using System.Diagnostics;
    using System.IO;
    using System.Text;
    using System.Windows.Forms;
    
    public partial class PluginBase
    {
        #region Fields

        string iniFilePath = string.Empty;

        frmConfigure dlgConf;

        #endregion
        
        #region " StartUp/CleanUp "
        void CommandMenuInit()
        {
            StringBuilder sbIniFilePath = new StringBuilder(Win32.MAX_PATH);
            Win32.SendMessage(nppData._nppHandle, NppMsg.NPPM_GETPLUGINSCONFIGDIR, Win32.MAX_PATH, sbIniFilePath);
            iniFilePath = sbIniFilePath.ToString();
             
            if (!Directory.Exists(iniFilePath))
            	Directory.CreateDirectory(iniFilePath);
             
         	iniFilePath = Path.Combine(iniFilePath, _pluginBaseName + ".ini");
             
            if (!File.Exists(iniFilePath))
            	File.Create(iniFilePath);
                        
        	SetCommand(0, "Compile", CompileCoffeeScript);
            SetCommand(1, "Configuration", SetConfiguration);
            
            var sbCoffePath = new StringBuilder(Win32.MAX_PATH);
             
            Win32.GetPrivateProfileString("Configuration", "CoffeeDir", string.Empty, sbCoffePath, Win32.MAX_PATH, iniFilePath);
            
            global.coffeeExecDir = sbCoffePath.ToString();
        }
        void SetToolBarIcon()
        {
            // toolbarIcons tbIcons = new toolbarIcons();
            // tbIcons.hToolbarBmp = tbBmp.GetHbitmap();
            // IntPtr pTbIcons = Marshal.AllocHGlobal(Marshal.SizeOf(tbIcons));
            // Marshal.StructureToPtr(tbIcons, pTbIcons, false);
            // Win32.SendMessage(nppData._nppHandle, NppMsg.NPPM_ADDTOOLBARICON, _funcItems.Items[idMyDlg]._cmdID, pTbIcons);
            // Marshal.FreeHGlobal(pTbIcons);
        }
        void PluginCleanUp()
        {
	        // Win32.WritePrivateProfileString("SomeSection", "SomeKey", someSetting ? "1" : "0", iniFilePath);
	        Win32.WritePrivateProfileString("Configuration", "CoffeeDir", global.coffeeExecDir, iniFilePath);
        }
        #endregion
        
        #region " Menu functions "
        // void myMenuFunction()
        // {
        // }
        // void myDockableDialog()
        // {
            // if (frmMyDlg == null)
            // {
                // frmMyDlg = new frmMyDlg(this);

                // NppTbData _nppTbData = new NppTbData();
                // _nppTbData.hClient = frmMyDlg.Handle;
                // _nppTbData.pszName = "My dockable dialog";
                // _nppTbData.dlgID = idMyDlg;
                // _nppTbData.uMask = NppTbMsg.DWS_DF_CONT_RIGHT;
                // _nppTbData.pszModuleName = _pluginModuleName;
                // IntPtr _ptrNppTbData = Marshal.AllocHGlobal(Marshal.SizeOf(_nppTbData));
                // Marshal.StructureToPtr(_nppTbData, _ptrNppTbData, false);

                // Win32.SendMessage(nppData._nppHandle, NppMsg.NPPM_DMMREGASDCKDLG, 0, _ptrNppTbData);
            // }
            // else
            // {
                // Win32.SendMessage(nppData._nppHandle, NppMsg.NPPM_DMMSHOW, 0, frmMyDlg.Handle);
            // }
        // }
        
        void SetConfiguration()
        {
        	if (dlgConf == null)
        		dlgConf = new frmConfigure(this);
        	
        	dlgConf.ShowDialog();
        }
        
        void CompileCoffeeScript()
        {
        	
        	
        	var path = new StringBuilder(Win32.MAX_PATH);
        	var fileName = new StringBuilder(Win32.MAX_PATH);
        	var fullPath = string.Empty;
        	Win32.SendMessage(nppData._nppHandle, NppMsg.NPPM_GETCURRENTDIRECTORY, Win32.MAX_PATH, path);
        	Win32.SendMessage(nppData._nppHandle, NppMsg.NPPM_GETFILENAME, Win32.MAX_PATH, fileName);
        	fullPath = "\"" + path.ToString() + "\\" +  fileName.ToString() + "\"";
        	
        	if (string.IsNullOrEmpty(global.coffeeExecDir))
        	{
        		MessageBox.Show("You have not configured this plug-in yet");
        		SetConfiguration();
        		return;
        	}
        	
        	try 
        	{
        		var proc = new Process();
        		proc.StartInfo.UseShellExecute = true;
        		proc.StartInfo.CreateNoWindow = false;
        		proc.StartInfo.FileName = global.coffeeExecDir + @"\coffee.exe";
        		proc.StartInfo.Arguments = "-c " + fullPath + " " + "-o \"" + path.ToString() + "\"";
        		proc.Start();
        	}
        	catch (Exception ex)
        	{
        		MessageBox.Show(ex.Message);
        	}
        }
        
        #endregion
    }
}